package com.hjk.waibao.mj.util;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description
 * @Author hjk
 * @Date 2020/7/13
 **/
public class ExcelColorReader {
    Map<String, String> colorMap = new HashMap<>();
    String extString;
    String filePath;
    File file;

    public ExcelColorReader(String filePath) {
        this.filePath = filePath;
        extString = filePath.substring(filePath.lastIndexOf("."));
        readExcel();
    }

    public void readExcel() {
        Sheet sheet = null;
        Row row = null;
        Row rowHeader = null;
        Workbook wb = null;
        if (filePath == null) {
            return;
        }
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                wb = new XSSFWorkbook(is);
            } else {
                wb = null;
            }
            if (wb != null) {
                // 获取第一个sheet
                sheet = wb.getSheetAt(0);
                // 获取最大行数
                int rownum = sheet.getPhysicalNumberOfRows();
                // 获取第一行
                rowHeader = sheet.getRow(0);
                row = sheet.getRow(0);
                // 获取最大列数
                int colnum = row.getPhysicalNumberOfCells();
                for (int i = 1; i < rownum; i++) {
                    Map<String, String> map = new LinkedHashMap<String, String>();
                    row = sheet.getRow(i);
                    if (row != null) {
                        for (int j = 0; j < colnum; j++) {
//                            if (columns[j].equals(getCellFormatValue(rowHeader.getCell(j)))) {
//                                cellData = (String) getCellFormatValue(row
//                                        .getCell(j));
//                                map.put(columns[j], cellData);
//                            }
                            String hexString = "FFFFFF";
                            if (".xls".equals(extString)) {
                                String tempHexString = ((HSSFColor) row.getCell(j).getCellStyle().getFillForegroundColorColor()).getHexString();
                                if (!"0:0:0".equalsIgnoreCase(tempHexString)) {
                                    String rString = tempHexString.substring(0, tempHexString.indexOf(":"));
                                    String gString = tempHexString.substring(tempHexString.indexOf(":") + 1, tempHexString.lastIndexOf(":"));
                                    String bString = tempHexString.substring(tempHexString.lastIndexOf(":") + 1);
                                    Integer r = Integer.valueOf(rString, 16) / 257;
                                    Integer g = Integer.valueOf(gString, 16) / 257;
                                    Integer b = Integer.valueOf(bString, 16) / 257;
                                    String rHex = Integer.toHexString(r).toUpperCase();
                                    if (rHex.length() == 1) {
                                        rHex = "0" + rHex;
                                    }
                                    String gHex = Integer.toHexString(g).toUpperCase();
                                    if (gHex.length() == 1) {
                                        gHex = "0" + gHex;
                                    }
                                    String bHex = Integer.toHexString(b).toUpperCase();
                                    if (bHex.length() == 1) {
                                        bHex = "0" + bHex;
                                    }
                                    hexString = rHex + gHex + bHex;
                                }
                            } else {
                                XSSFColor xssfColor = ((XSSFColor) row.getCell(j).getCellStyle().getFillForegroundColorColor());
                                if (xssfColor != null) {
                                    hexString = xssfColor.getARGBHex().substring(2);
                                }
                            }
                            colorMap.put(getKey(i, j), hexString);
                        }
                    } else {
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getKey(Integer row, Integer col) {
        return row + "," + col;
    }

    public String get(Integer row, Integer col) {
        return colorMap.get(getKey(row, col));
    }

    public static void main(String[] args) {
        new ExcelColorReader("C:\\Users\\svx\\Desktop\\商标模板1.xlsx");
    }


}
